package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.constant.RedisConstant;
import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.common.utils.JwtUtil;
import com.atguigu.lease.model.entity.SystemUser;
import com.atguigu.lease.model.enums.BaseStatus;
import com.atguigu.lease.web.admin.service.LoginService;
import com.atguigu.lease.web.admin.service.SystemUserService;
import com.atguigu.lease.web.admin.vo.login.CaptchaVo;
import com.atguigu.lease.web.admin.vo.login.LoginVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wf.captcha.SpecCaptcha;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Autowired
    private SystemUserService service;
    @Override
    public CaptchaVo getcaptch() {
        /*//1.创建验证码图片的工具类
        SpecCaptcha specCaptcha = new SpecCaptcha(521, 134, 2);
        //2.生成验证码图片的base64和验证码正确字符串
        String codeString = specCaptcha.text();
        String codebase64 = specCaptcha.toBase64();
        //3.正确字符串+uuid+有效时间存储到redis中
        String key = RedisConstant.ADMIN_LOGIN_PREFIX+ UUID.randomUUID();
        redisTemplate.opsForValue().set(key,codeString,RedisConstant.ADMIN_LOGIN_CAPTCHA_TTL_SEC, TimeUnit.SECONDS);
        //4.uuidkey和验证码图片封装在catptchaVo中
        return new CaptchaVo(codebase64,key);*/
        //1.创建图片验证码图片的工具类
        SpecCaptcha specCaptcha = new SpecCaptcha(148, 30, 4);
        //2.生成验证码图片的base64和验证码字符串的生成
        String text = specCaptcha.text();
        String base64 = specCaptcha.toBase64();
        //3.将字符串+uuid+有效时间加入到redis中
        String key = RedisConstant.ADMIN_LOGIN_PREFIX + UUID.randomUUID();
        redisTemplate.opsForValue().set(key,text,RedisConstant.ADMIN_LOGIN_CAPTCHA_TTL_SEC,TimeUnit.SECONDS);
        //4.uuid的key和验证码图片封装到和工具类
        return new CaptchaVo(base64,key);

    }

    //登录
    @Override
    public String login(LoginVo loginVo) {
        /*//1.检查验证码的内容
        //1.1输入验证码是否为空
        if(ObjectUtils.isEmpty(loginVo.getCaptchaCode())){
            throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_NOT_FOUND);
        }
        //1.2检查redis中是否过期
        //拿到redis中的验证码
        String codeString = (String) redisTemplate.opsForValue().get(loginVo.getCaptchaKey());
        if(ObjectUtils.isEmpty(codeString)){
            throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_EXPIRED);
        }
        //1.3检查输入的验证码和redis中的验证码比较
        if(!codeString.equalsIgnoreCase(loginVo.getCaptchaCode())){
            throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_ERROR);
        }
        //2.检查账号和密码内容
        //2.1检查账号是否正确
        LambdaQueryWrapper<SystemUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SystemUser::getUsername,loginVo.getUsername());
        SystemUser systemUser = service.getOne(wrapper);
        if(systemUser==null){
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_NOT_EXIST_ERROR);
        }
        //2.2密码对比
        //对输入的密码进行加密再和数据库中的做比较
        String pwd = DigestUtils.md5Hex(loginVo.getPassword());
        if(!systemUser.getPassword().equals(pwd)){
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_ERROR);
        }

        //2.3.检查账号是否可用
        if(systemUser.getStatus()==BaseStatus.DISABLE){
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_DISABLED_ERROR);
        }
        //3.生成token
        return JwtUtil.createToken(systemUser.getId(),systemUser.getUsername());*/

        //1.检查验证码是否为空
        //1.1验证码是否
        if(ObjectUtils.isEmpty(loginVo.getCaptchaCode())){
            throw new LeaseException(ResultCodeEnum.APP_LOGIN_CODE_EMPTY);
        }
        //1.2redis中的验证码是否过期
        String code = (String) redisTemplate.opsForValue().get(loginVo.getCaptchaKey());
        if(ObjectUtils.isEmpty(code)){
            throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_EXPIRED);
        }
        //1.3检查输入的验证码和redis中的验证码是否相等
        if(!code.equalsIgnoreCase(loginVo.getCaptchaCode())){
            throw new LeaseException(ResultCodeEnum.APP_LOGIN_CODE_ERROR);
        }
        //2.检查账号密码
        //2.1账号是否正确
        LambdaQueryWrapper<SystemUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SystemUser::getUsername,loginVo.getUsername());
        //查询数据库存在的这个账号
        SystemUser systemUser = service.getOne(wrapper);
        if(systemUser==null){
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_NOT_EXIST_ERROR);
        }
        //2.2查询密码是否正确
        String password = DigestUtils.md5Hex(loginVo.getPassword());
        if(!systemUser.getPassword().equals(password)){
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_ERROR);
        }
        //2.3查看账号状态
        if(systemUser.getStatus()==BaseStatus.DISABLE){
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_DISABLED_ERROR);
        }
        return JwtUtil.createToken(systemUser.getId(),systemUser.getUsername());
    }
}
